<?php

class Ebookers {

    private $username = "06160f5dd7e447f3ac2a912d70316497"; //Access ID
    private $password = "b30b2c61367443599560139cd358063c"; //Access Key
    private $serviceUrl = "https://ws.orbitzworldwide.com/xml/service";
    private $search;
    private $xml;

    public function Ebookers($searchQuery) {
        $this->search = $searchQuery;
    }

    public function getTickets($index) {
        $this->buildXml();
        $results = $this->sendRequest($this->xmlRequest);
		echo "<pre>";
        print_r($results);
		echo "Hoi</pre>";
	  //print_r($this->xmlRequest);
    }
    
    
    private function buildXml(){      

        $deptDate =  date('m-d-Y', strtotime($this->search->getDepartureDate()));
        $deptDate =  (string)$deptDate;
	
         $this->xmlRequest = '<?xml version="1.0" encoding="UTF-8"?>
            <AirShoppingRequest xmlns="http://ws.orbitz.com/schemas/2008/Air">
                <PointOfSale>EBNL</PointOfSale>
                <Travelers>
                    <Adults>'.$this->search->getAdults().'</Adults>
                </Travelers>
                <Slices preferNonStop="false" refundableOnly="false">
                    <CabinType code="C"/>
                    <Slice>
                        <Departure>
                            <Airport code='.$this->search->getDeparturyCityIATA().'/>
                            <Date>2012-11-08</Date>
                        </Departure>
                        <Arrival>
                            <Airport code="'.$this->search->getDestinationCityIATA().'"/>
                        </Arrival>
                    </Slice>
                    <Slice>
                        <Departure>
                            <Airport code="'.$this->search->getDestinationCityIATA().'"/>
                            <Date>'.$this->search->getReturnDate().'</Date>
                        </Departure>
                        <Arrival>
                            <Airport code="'.$this->search->getDeparturyCityIATA().'"/>
                        </Arrival>
                    </Slice>
                </Slices>
            </AirShoppingRequest>';
    }
    
    private function sendRequest($xml){
        
        $lengh = strlen($xml);

        $HEADERS = array('Accept: */*',
                         'Content-Type: application/xml; charset=utf-8',
                         "Content-Length: $lengh"
                         ); 

        // The usual - init a curl session and set the url
        $ch = curl_init($this->serviceUrl); //curl header
        //curl_setopt($ch, CURLOPT_URL, $SERVICE_URL); //did not work

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $HEADERS ); 

        // Set your login and password for authentication
        curl_setopt($ch, CURLOPT_USERPWD, "$this->username:$this->password");

        // You can use CURLAUTH_BASIC, CURLAUTH_DIGEST, CURLAUTH_GSSNEGOTIATE,
        // CURLAUTH_NTLM, CURLAUTH_ANY, and CURLAUTH_ANYSAFE
        //
        // You can use the bitwise | (or) operator to combine more than one method.
        // If you do this, CURL will poll the server to see what methods it supports and pick the best one.
        //
        // CURLAUTH_ANY is an alias for CURLAUTH_BASIC | CURLAUTH_DIGEST |
        // CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM
        //
        // CURLAUTH_ANYSAFE is an alias for CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE |
        // CURLAUTH_NTLM
        //
        // Personally I prefer CURLAUTH_ANY as it covers all bases
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

        // This is occassionally required to stop CURL from verifying the peer's certificate.
        // CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if
        // CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2 - check the existence of a
        // common name and also verify that it matches the hostname provided)
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // Optional: Return the result instead of printing it
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 

        // The usual - get the data and close the session
        $data = curl_exec($ch);
        $xml = simplexml_load_string($data);
		print_r($xml);
        curl_close($ch); 

        //header('Content-Type: application/xml; charset=UTF-8');
        //print_r($data);
        return $data;
    }
    
}
 
